package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.SysOrganization;
import com.caifu.pojo.SysUser;
import com.caifu.service.SysOrganizationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tww
 * ClassName: SysOrganizationController.java、
 * createTime: 2021年03月02日 09:58:40
 **/
@Controller
@RequestMapping("organization")
public class SysOrganizationController extends AbstractController {

    private static final Logger logger = LoggerFactory.getLogger(SysOrganizationController.class);


    @Resource
    private SysOrganizationService sysOrganizationService;


    private String prefix = "organization/";

    /**
     * 组织架构页
     */
    @GetMapping("toSysOrganizationPage")
    public String toSysOrganizationPage(Model model) {
        SysUser sysUser = getSysUser();
        //组织架构
        List<SysOrganization> orgList = sysOrganizationService.getOrgList(sysUser);
        model.addAttribute("orgLists", orgList);
        return prefix + "organization";
    }

    /**
     * 组织添加页
     */
    @GetMapping("toOrgAddPage")
    public String toOrgAddPage(Model model) {
        SysUser sysUser = getSysUser();
        //组织架构
        List<SysOrganization> orgList = sysOrganizationService.getOrgList(sysUser);
        model.addAttribute("orgLists", orgList);
        return prefix + "organization_add";
    }

    /**
     * 组织添加页
     */
    @GetMapping("toOrgUpdatePage")
    public String toOrgUpdatePage(Integer orgId, Model model) {
        SysUser sysUser = getSysUser();
        //组织架构
        List<SysOrganization> orgList = sysOrganizationService.getOrgList(sysUser);
        model.addAttribute("orgLists", orgList);

        model.addAttribute("orgId", orgId);
        //当前组织
        SysOrganization org = sysOrganizationService.getOrgById(orgId);
        model.addAttribute("org", org);

        //当前组织的上级组织
        SysOrganization pOrg = sysOrganizationService.getPOrgById(org);
        model.addAttribute("pOrg", pOrg);
        return prefix + "organization_update";
    }

    /**
     * 添加
     */
    @PostMapping("save")
    @ResponseBody
    public Result save(SysOrganization sysOrganization) {
        return sysOrganizationService.save(sysOrganization);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @ResponseBody
    public Result update(SysOrganization sysOrganization) {
        return sysOrganizationService.update(sysOrganization);
    }


    /**
     * 根据用户id获取组织树
     *
     * @return
     */
    @GetMapping("getOrgList")
    @ResponseBody
    public Result getOrgList() {
        SysUser sysUser = getSysUser();
        List<SysOrganization> orgList = sysOrganizationService.getOrgList(sysUser);
        return ResultUtil.getDataSuccess(orgList);
    }

    /**
     * 根据id删除树 （无子节点）
     */
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(Integer nodeId) {
        return sysOrganizationService.deleteById(nodeId);
    }


}
